package com.ihg.hiex.admin.web.controller.account;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Preloading;
import com.demo2do.core.web.utils.WebUtils;
import com.ihg.hiex.admin.entity.search.AdministratorSearch;
import com.ihg.hiex.admin.service.account.AdministratorService;
import com.ihg.hiex.admin.service.session.ConferenceRoomService;
import com.ihg.hiex.entity.data.conference.ConferenceRoom;
import com.ihg.hiex.entity.data.conference.Type;
import com.ihg.hiex.entity.user.Administrator;
import com.ihg.hiex.entity.user.Role;

/**
 * 管理员
 * 
 * @author houtao
 *
 */
@Controller
@RequestMapping("/administrators")
@MenuSetting("menu-settings")
public class AdministratorController {
    
    private static final Logger logger = LoggerFactory.getLogger(AdministratorController.class);
    
    @Autowired
    private AdministratorService administratorService;
    
    @Autowired
    private ConferenceRoomService conferenceRoomService;
    
    /**
     * list all the administrator by page
     * 
     * @param page
     * @return
     */
    @MenuSetting("submenu-administrator-management")
    @RequestMapping(value = "" , method = RequestMethod.GET)
    public ModelAndView list(Page page) {
        
        ModelAndView modelAndView = new ModelAndView("account/administrator/administrator-list");
        
        int totals = administratorService.count(Role.ADMIN.toString());
        if (totals > 0) {

            List<Administrator> administrators = administratorService.list(Role.ADMIN.toString(), page.with(totals));
            
            modelAndView.addObject("administrators", administrators);
            modelAndView.addObject("page", page);
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * display search panel
     * 
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public String search() {
        return "account/administrator/administrator-search";
    }
    
    /**
     * search administrator by page
     * 
     * @param search
     * @param page
     * @param request
     * @return
     */
    @MenuSetting("submenu-administrator-management")
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    public ModelAndView onSearch(AdministratorSearch search, Page page, HttpServletRequest request) {
        
        ModelAndView modelAndView = new ModelAndView("account/administrator/administrator-list");
        
        int totals = administratorService.count(Administrator.class, search.getPreparedFilter(false));
        if (totals > 0) {
            
            List<Administrator> administrators = administratorService.search(Administrator.class, search, page.with(totals));
            modelAndView.addObject("administrators", administrators);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "filter","page"));
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * view administrator
     * 
     * @param id
     * @return
     */
    @MenuSetting("submenu-administrator-management")
    @RequestMapping(value = "/{id}" , method = RequestMethod.GET)
    public ModelAndView view(@PathVariable("id") Long id) {
        
        ModelAndView modelAndView = new ModelAndView("account/administrator/administrator-view");
        
        Administrator administrator = administratorService.load(Administrator.class, id);
        
        modelAndView.addObject("administrator", administrator);
        
        return modelAndView;
        
    }
    
    /**
     * create administrator panel
     * 
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public ModelAndView create() {
        
        ModelAndView modelAndView = new ModelAndView("account/administrator/administrator-create");
        
        List<ConferenceRoom> rooms = conferenceRoomService.list(Type.WebEx);
        modelAndView.addObject("rooms", rooms);
        
        return modelAndView;
        
    }
    
    /**
     * create administrator
     * 
     * @param administrator
     * @param conferenceRoomId
     * @return
     */
    @RequestMapping(value = "" , method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onCreate(Administrator administrator, @RequestParam("conferenceRoomId") Long conferenceRoomId) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            ConferenceRoom conferenceRoom = conferenceRoomService.load(ConferenceRoom.class, conferenceRoomId);
            administratorService.create(administrator, conferenceRoom);
            
            httpResult.success().message("增加管理员成功");
            
        } catch (Exception e) {
            httpResult.fail("500").message("增加管理员失败");
            logger.error("#onCreate() - create fail, exception is [{}]",e);
        }
        
        return httpResult;
        
    }
    
    /**
     * prepare to edit administrator
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("id") Long id) {
        
        ModelAndView modelAndView = new ModelAndView("account/administrator/administrator-edit");
        
        Administrator administrator = administratorService.load(Administrator.class, id);
        
        modelAndView.addObject("administrator", administrator);
        
        return modelAndView;
        
    }
    
    /**
     * edit an administrator
     * 
     * @param id
     * @param administrator
     * @return
     */
    @RequestMapping(value = "/{id}" , method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onEdit(@PathVariable(name="id") Long id, @Preloading Administrator administrator) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            administratorService.edit(administrator);
            
            httpResult.success().message("编辑管理员成功");
            
        } catch (Exception e) {
            httpResult.fail("500").message("编辑管理员失败");
            logger.error("#onEdit() - edit fail, exception is [{}]",e);
        }
        
        return httpResult;
        
    }
    
    /**
     * display delete panel
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
    public ModelAndView delete(@PathVariable("id") Long id) {
    	
    	ModelAndView modelAndView = new ModelAndView("account/administrator/administrator-delete");

        modelAndView.addObject("id", id);
        
        return modelAndView;
        
    }
    
    /**
     * delete an administrator
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onDetele(@PathVariable("id") Long id) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            Administrator administrator = administratorService.load(Administrator.class, id);
            administratorService.disable(administrator);
            
            httpResult.success().message("删除管理员成功");
            
        } catch (Exception e) {
            httpResult.fail("500").message("删除管理员失败");
            logger.error("#onDelete() - delete fail, exception is [{}]",e);
        }
        
        return httpResult;
        
    }
    
}